package cassandrawindow;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HandleDataTableDomainDate {
    public static final String KEYSPACE                         =   "AdsMobileDataResultKs";
    
    public static final String ColumFamlyKeyDomain              =   "CampaignBannerDomainViewedDataCf";
    public static final String ColumFamlyDataClickDomain        =   "MobileCampaignBannerDomainCountCf";
    public static final String ColumFamlyDataViewDomain         =   "MobileCampaignBannerDomainCountViewCf";
    
    public static long checkDateDomainDate = 0;
    
    public void handleData() throws ParseException, InterruptedException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {        
        long first_time = System.currentTimeMillis();
        
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);         
        String currentDate = HandleDataTableDomainDate.handleDate();
        //String currentDate = "2013-08-12";
        Debug.println("ngay chay la:"+currentDate);
        //cassAct.getDomaindate(currentDate, ColumFamlyDomainDateCf, mysql, mysqlMain);
        //cassAct.getDomaindateMobile(currentDate, BzSuperBannerDomain6HDateCf, mysql, mysqlMain);
        long startTime = TimeUtil.longToDateWithHourBanner("06:00:00 "+currentDate);        
        long finishTime = startTime + 24*60*60*1000 - 1;        
        cassAct.getDataMobile(currentDate, ColumFamlyKeyDomain,ColumFamlyDataClickDomain,ColumFamlyDataViewDomain,startTime,finishTime,currentDate);
        
        long last_time = System.currentTimeMillis();
        Debug.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableDomainDate");        
    }
        
    /*
     * Lay thoi gian tu 6h sang ngay hom truoc toi 5h59 sang ngay hom sau
     * Coi la 1 ngay
     */
    public static String handleDate()
    {
        String retDate = "";//2012-07-24
        String date = TimeUtil.getCurrentTimeInStringForLog();//yyyy/MM/dd H:mm:ss
        
        String[] arrTime        = new String[2];
        String[] firstArrTime   = new String[3];
        String[] secondArrTime  = new String[3];

        arrTime = date.split(" ");
        firstArrTime    = arrTime[1].split(":");
        secondArrTime   = arrTime[0].split("-");
        if(Integer.valueOf(firstArrTime[0]) >= 6 && Integer.valueOf(firstArrTime[0]) <= 23)
        {            
            retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+secondArrTime[2];
        }
        //init checkDate if trigger from 0h -> 6h
        else if (Integer.valueOf(firstArrTime[0]) >= 0 && Integer.valueOf(firstArrTime[0]) < 6)
        {            
            long unixtime =System.currentTimeMillis();
            long unixtimeLastDay = unixtime - 24*60*60*1000;
            retDate = TimeUtil.getTimeInStringForCronjob(unixtimeLastDay);
            
            /*
            int day = Integer.valueOf(secondArrTime[2]) - 1;
            
            if(day < 10){
                retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+"0"+String.valueOf(day);
            } else{
                retDate = secondArrTime[0]+"-"+secondArrTime[1]+"-"+String.valueOf(day);
            }*/
        }        
        return retDate;
    }
}
